package top.leetcode;

import java.util.HashMap;

/**
 * @Desc
 * @Author houruiyang
 * @Date 2022/12/20
 **/
public class _003 {
    public static void main(String[] args) {
        int pwwkew = lengthOfLongestSubstring("abcbcbb");
        System.out.println(pwwkew);
    }
    public static int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) {
            return 0;
        }
        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0;
        int left = 0;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                left = Math.max(left, map.get(s.charAt(i)) + 1); // 下标移动
            }
            map.put(s.charAt(i), i);
            max = Math.max(max, i - left + 1); //+1 类似 i++
        }
        return max;

    }

}
